package indi.conastin.textmodify.adapter

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.text.TextWatcher
import android.util.Base64
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import indi.conastin.textmodify.databinding.AppListListviewItemBinding
import indi.conastin.textmodify.databinding.AppMethodListviewItemBinding
import indi.conastin.textmodify.databse.HookMethodEntity
import indi.conastin.textmodify.viewmodel.InventoryViewModel
import java.io.File
import java.io.FileOutputStream

class AppMethodAdapter(private val onRuleLongClicked: (HookMethodEntity) -> Unit) :
    ListAdapter<HookMethodEntity, AppMethodAdapter.PackageViewHolder>(DiffCallback) {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): PackageViewHolder {
        return PackageViewHolder(
            AppMethodListviewItemBinding.inflate(
                LayoutInflater.from(
                    parent.context
                ),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: PackageViewHolder, position: Int) {
        val current = getItem(position)
        holder.itemView.setOnClickListener {
            onRuleLongClicked(current)
        }
        holder.bind(current)
    }

    class PackageViewHolder(private var binding: AppMethodListviewItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(packageInfoEntity: HookMethodEntity) {
            binding.originText.text = packageInfoEntity.originText
            binding.newText.text = packageInfoEntity.newText
        }
    }

    companion object {
        private val DiffCallback = object : DiffUtil.ItemCallback<HookMethodEntity>() {
            override fun areItemsTheSame(
                oldItem: HookMethodEntity,
                newItem: HookMethodEntity
            ): Boolean {
                return oldItem === newItem
            }

            override fun areContentsTheSame(
                oldItem: HookMethodEntity,
                newItem: HookMethodEntity
            ): Boolean {
                return oldItem.originText == newItem.originText
            }
        }
    }

}